package com.sxbbc.bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.core.entity.banks.SysProductType;
import com.sxbbc.common.core.mapper.banks.SysProductTypeMapper;
import com.sxbbc.bank.service.ISysProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 产品类型表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-09-04
 */
@Service
public class SysProductTypeServiceImpl extends ServiceImpl<SysProductTypeMapper, SysProductType> implements ISysProductTypeService {


    private static Logger log = LogManager.getLogger(SysProductTypeServiceImpl.class);

    @Autowired
    private SysProductTypeMapper sysProductTypeMapper;

    @Override
    public RestResponse selSysProductType(BasePage<SysProductType> page, SysProductType sysProductType, String yyTime) {
        log.info("执行【产品展示】产品类型列表查询接口");
        log.info("分页参数{size"+page.getSize()+"，current"+page.getCurrent()+"，pages"+page.getPages()+"}");
        log.info("对象"+sysProductType);
        log.info("查询时间"+yyTime);
        QueryWrapper<SysProductType> wrapper = new QueryWrapper<>();
        if(null!=sysProductType){
            if(StringUtils.isNotEmpty(sysProductType.getProdName())){
                wrapper.like("prod_name",sysProductType.getProdName());
            }
            if(StringUtils.isNotEmpty(sysProductType.getProdCode())){
                wrapper.like("prod_code",sysProductType.getProdCode());
            }
            if (StringUtils.isNotEmpty(sysProductType.getTypeGrade())){
                wrapper.eq("type_grade",sysProductType.getTypeGrade());
            }
            if (null!=sysProductType.getStatus()){
                wrapper.eq("status",sysProductType.getStatus());
            }
            if(null!=yyTime && yyTime.trim().length()>0){
                String [] arr=yyTime.split("~");
                wrapper.between("create_time",arr[0],arr[1]);
            }
        }
        super.page(page,wrapper);
        try {
            return RestFactory.success(page.getRecords(), page.getTotal());
        } catch (Exception e) {
            log.error("【产品展示】产品类型列表查询异常："+e.getMessage());
            throw new ThrowJsonException("列表查询异常");
        }
    }

    @Override
    public com.function.common.RestResponse addSysProductType(SysProductType sysProductType) {
        log.info("执行【产品展示】添加产品展示类型");
        log.info("对象"+sysProductType);
        QueryWrapper<SysProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("prod_name",sysProductType.getProdName()).or().eq("prod_code",sysProductType.getProdCode());
        List<SysProductType> sysProductTypeList = sysProductTypeMapper.selectList(wrapper);
        if (sysProductTypeList.size()>0){
            return GetRest.getFail("该类型或该编码已存在，请勿重复添加");
        }
        boolean saved;
        try {
            saved = super.save(sysProductType);
        } catch (Exception e) {
            log.error("【产品展示】添加产品展示类型异常："+e.getMessage());
            throw new ThrowJsonException("添加产品展示类型异常");
        }
        if (!saved) {
            return GetRest.getFail("保存失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    @Override
    public com.function.common.RestResponse updateSysProductType(SysProductType sysProductType) {
        log.info("执行【产品展示】修改产品展示类型");
        log.info("对象"+sysProductType);
        SysProductType selSysProductType = super.getById(sysProductType.getId());
        if (null == selSysProductType){
            return GetRest.getFail("该产品类型不存在");
        }
        QueryWrapper<SysProductType> wrapper = new QueryWrapper<>();
        wrapper.nested(i ->i.eq("prod_name",sysProductType.getProdName()).or().eq("prod_code",sysProductType.getProdCode())).ne("id",selSysProductType.getId());

        List<SysProductType> sysProductTypeList = sysProductTypeMapper.selectList(wrapper);
        if (sysProductTypeList.size()>0){
            return GetRest.getFail("该类型或该编码已存在，请勿重复添加");
        }
        boolean saved;
        try {
            saved = super.updateById(sysProductType);
        } catch (Exception e) {
            log.error("【产品展示】修改产品展示类型异常："+e.getMessage());
            throw new ThrowJsonException("修改产品展示类型异常");
        }
        if (!saved) {
            return GetRest.getFail("保存失败");
        }
        return GetRest.getSuccess("保存成功");

    }
}
